import spaceObject
import turret
import nodemanager
import uthread
import trinity
import sys
import blue

class Billboard(spaceObject.SpaceObject):
    __guid__ = 'spaceObject.Billboard'

    def __init__(self):
        spaceObject.SpaceObject.__init__(self)
        self.fitted = False



    def Assemble(self):
        self.UnSync()
        slimItem = sm.StartService('michelle').GetBallpark().GetInvItem(self.id)
        godmaStateManager = sm.StartService('godma').GetStateManager()
        godmaType = godmaStateManager.GetType(slimItem.typeID)
        self.gfxTurretID = godmaType.gfxTurretID
        if ((self.model is not None) and (not self.HasBlueInterface(self.model, 'IEveSpaceObject2'))):
            self.FitHardpoints()
        self.fitted = True
        uthread.pool('Billboard::LateAssembleUpdate', self.LateAssembleUpdate)



    def LateAssembleUpdate(self):
        billboardSvc = sm.GetService('billboard')
        billboardSvc.Update(self)



    def FitHardpoints(self):
        if self.fitted:
            return 
        if (self.model is None):
            self.LogWarn('FitHardpoints - No model')
            return 
        turrets = nodemanager.FindNodes(self.model, 'locator_turret', 'trinity.TriSplTransform')
        self.modules = {}
        slotcounter = 0
        turretPair = turret.TurretPair()
        turretPair.Initialize(self.gfxTurretID)
        turretPair.owner = self
        turretPair.turrets[0].owner = self
        turretPair.turrets[1].owner = self
        hardpoint1 = turrets[0]
        hardpoint2 = turrets[1]
        hardpoint1.object = turretPair.turrets[0].triTurret
        hardpoint2.object = turretPair.turrets[1].triTurret
        turretPair.turrets[0].owner = hardpoint1
        turretPair.turrets[1].owner = hardpoint2
        self.modules[self.id] = turretPair
        for each in turretPair.turrets:
            each.guntype = 'e'

        self.fitted = True



    def SetMap(self, name, path, idx = 0):
        if (path is None):
            return 
        self.LogInfo('Setting', name, 'to', path)
        self.model.FreezeHighDetailMesh()
        try:
            textureParameters = self.model.Find('trinity.TriTexture2DParameter')
            texture = [ x for x in textureParameters if (x.name == name) ][idx]
            texture.resourcePath = path
        except (Exception), e:
            self.LogError('SetMap() - Error updating billboard map', name, path, e)
            sys.exc_clear()



    def UpdateBillboardContents(self, advertPath, facePath):
        self.LogInfo('UpdateBillboardWithPictureAndText:', advertPath, facePath)
        if (self.model is None):
            return 
        self.SetMap('FaceMap', facePath)
        self.SetMap('AdvertMap', advertPath)
        self.SetMap('DiffuseMap', 'cache:/Temp/headlines.dds', 1)
        self.SetMap('DiffuseMap', 'cache:/Temp/bounty_caption.dds')



    def Release(self):
        if self.released:
            return 
        if hasattr(self, 'modules'):
            for turretPair in self.modules.itervalues():
                turretPair.owner = None
                turretPair.Release()
                for turret in turretPair.turrets:
                    turret.owner = None


        spaceObject.SpaceObject.Release(self)



exports = {'spaceObject.Billboard': Billboard}

